import os
from PIL import Image, ImageDraw, ImageFont

def create_placeholder_image(path, width=1920, height=1080, text="图像生成失败，请重试"):
    """创建一个占位图像"""
    img = Image.new('RGB', (width, height), color=(255, 255, 255))
    draw = ImageDraw.Draw(img)
    
    try:
        # 尝试加载字体
        font = ImageFont.truetype("arial.ttf", 36)
    except:
        # 使用默认字体
        font = ImageFont.load_default()
    
    try:
        text_width = draw.textlength(text, font=font)
        position = ((width - text_width) // 2, height // 2)
        draw.text(position, text, fill=(0, 0, 0), font=font)
    except AttributeError:
        # 对于较旧版本的PIL，textlength可能不可用
        position = (width // 2, height // 2)
        draw.text(position, text, fill=(0, 0, 0), font=font, anchor="mm")
    
    # 确保目录存在
    os.makedirs(os.path.dirname(path), exist_ok=True)
    img.save(path)
    
    return path